package tictactoe;

import java.awt.*;
import java.awt.Font.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

import tictactoe.screens.*;
import tictactoe.types.*;

/**
 *
 * @author Ryan Falls
 */
public class TicTacToe extends JPanel
{
    //Constants
    protected final int borderSize = 8;
    public final static String TITLE = "Tic Tac Toe";
    //Private Variables
    private JMenuBar menuBar;
    private JLabel titleLabel;
    private JPanel screenPanel;
    private CardLayout screens = new CardLayout();
    private JPanel bottomPanel;
    public Font menuTitleFont = new Font("Serif", Font.PLAIN, 32);
    public Font menuItemFont = new Font("Serif", Font.PLAIN, 18);
    public final Border emptyBorder = new EmptyBorder(borderSize, borderSize, borderSize, borderSize);
    //Public Variables
    public Style[] styles = null;
    public Style currentStyle = null;
    //Screens
    public MainMenuScreen MainMenu;
    public OptionsMenuScreen Options;
    public GameScreen Game;

    /**
     * Main Method
     * Creates window, sets properties, and initializes it, then sets to visible.
     */
    public static void main(String[] args)
    {
	JFrame window = new JFrame(TITLE);
	// TODO code application logic here
	window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	window.setContentPane(new TicTacToe(window));
	window.pack();
	window.setVisible(true);
    }

    //Creates and populates the main window
    TicTacToe(JFrame window)
    {
	setLayout(new BorderLayout());
	window.setJMenuBar(createMenuBar());

	titleLabel = new JLabel(TITLE);
	titleLabel.setFont(new Font("Serif", Font.PLAIN, 50));
	titleLabel.setHorizontalAlignment(SwingConstants.CENTER);
	add(titleLabel, BorderLayout.NORTH);

	screenPanel = new JPanel(screens);
	add(screenPanel, BorderLayout.CENTER);

	//Add Menus
	MainMenu = new MainMenuScreen(this);
	screenPanel.add(MainMenu, Screens.mainMenu.name());

	//screenPanel.add(new AltMainMenuScreen(this),Screens.altMainMenu.name());
	Options = new OptionsMenuScreen(this);
	screenPanel.add(Options, Screens.optionsMenu.name());

	Game = new GameScreen(this);
	Game.RedrawBoard();
	screenPanel.add(Game, Screens.gameScreen.name());

	bottomPanel = new JPanel();
	add(bottomPanel, BorderLayout.SOUTH);
    }

    //Menu Bar
    private JMenuBar createMenuBar()
    {
	menuBar = new JMenuBar();
	JMenu menu;
	JMenuItem menuItem;

	//Create & Populate File Menu
	menu = new JMenu("File");
	menu.setMnemonic(KeyEvent.VK_F);
	//TODO Add Commands
	menuItem = new JMenuItem("Main Menu");
	menuItem.addActionListener(new ActionListener()
	{

	    public void actionPerformed(ActionEvent e)
	    {
		ChangeScreen(Screens.mainMenu);
	    }
	});
	menuItem.setVisible(false);
	menu.add(menuItem);
	menuItem = new JMenuItem("Exit");
	menuItem.addActionListener(new ActionListener()
	{

	    public void actionPerformed(ActionEvent e)
	    {
		AskExit();
	    }
	});
	menu.add(menuItem);
	menuBar.add(menu);

	menu = new JMenu("Help");
	menu.setMnemonic(KeyEvent.VK_H);
	menuBar.add(menu);

	return menuBar;
    }

    // Ask user if he is sure he wants to exit, and exit if so
    /**
     * Asks the user if they want to exit, then exits if they say yes.
     */
    public static void AskExit()
    {
	//TODO ask user if he wants to exit
	System.exit(0);
    }

    /**
     * Used to change screens.
     * @param screen A Screen enum value indicating which screen to change to.
     */
    public void ChangeScreen(Screens screen)
    {
	screens.show(screenPanel, screen.name());
	if(screen == Screens.mainMenu)
	{
	    // Sets the Main Menu to display the alternate option state
	    // when there is an active game.
	    MainMenu.SetAlternate(this.Game.GetGameState());
	    menuBar.getMenu(0).getMenuComponent(0).setVisible(false);
	}
	else
	{
	    menuBar.getMenu(0).getMenuComponent(0).setVisible(true);
	}
    }

    /**
     * Sets the bottom panel to the passed in panel.
     * @param newPane A JPanel object representing the new bottom panel.
     */
    public void SetBottomPanel(JPanel newBottomPanel)
    {
	bottomPanel = newBottomPanel;
    }
}
